﻿#include "ServerLevelDescEx.h"

#include <DescStore/ServerLevelDesc.h>

ServerLevelDescEx::ServerLevelDescEx()
{
	if (EN_OBJ_MODE_INIT == NFShmMgr::Instance()->GetCreateMode()) {
		CreateInit();
	}
	else {
		ResumeInit();
	}
}

ServerLevelDescEx::~ServerLevelDescEx()
{
}

int ServerLevelDescEx::CreateInit()
{
	return 0;
}

int ServerLevelDescEx::ResumeInit()
{
	return 0;
}

int ServerLevelDescEx::Load()
{

	return 0;
}

int ServerLevelDescEx::CheckWhenAllDataLoaded()
{
	m_aiDaysIndex.clear();
	m_aiDaysIndex.resize(m_aiDaysIndex.max_size(), INVALID_ID);
	for(int i = 0; i < (int)ServerLevelDesc::Instance()->GetResNum(); ++i)
	{
		E_ServerLevel* pDesc = ServerLevelDesc::Instance()->GetDescByIndex(i);
		CHECK_NULL(0, pDesc);
		CHECK_EXPR(pDesc->server_open_days >= 0 && pDesc->server_open_days < MAX_SERVER_LEVEL_DAYS_NUM, -1, "(pDesc->server_open_days:{} invalid", pDesc->server_open_days);
		m_aiDaysIndex[pDesc->server_open_days] = i;
	}

	int iPreIdx = 0;
	for (int i = 0; i < (int)m_aiDaysIndex.size(); ++i)
	{
		int iIdx = m_aiDaysIndex[i];
		if (iIdx >= 0)
		{
			iPreIdx = iIdx;
		}
		else
		{
			m_aiDaysIndex[i] = iPreIdx;
		}
	}

	for(int i = 1; i < (int)m_aiDaysIndex.size(); ++i)
	{
		int iIdx = m_aiDaysIndex[i];
		int iPreIdx = m_aiDaysIndex[i - 1];
		CHECK_EXPR(iPreIdx >= 0 && iPreIdx < (int)m_aiDaysIndex.size(), -1, "invalid server level desc idx: {}", iPreIdx);
		CHECK_EXPR(iIdx >= 0 && iIdx < (int)m_aiDaysIndex.size(), -1, "invalid server level desc idx: {}", iIdx);

		E_ServerLevel* pDesc = ServerLevelDesc::Instance()->GetDescByIndex(iIdx);
		CHECK_NULL(0, pDesc);
		E_ServerLevel* pPreDesc = ServerLevelDesc::Instance()->GetDescByIndex(iPreIdx);
		CHECK_NULL(0, pPreDesc);

		CHECK_EXPR(pDesc->server_level >= pPreDesc->server_level, -1, "server level must increase");

		LOGSVR_TRACE("days: " << i <<
			" server level: " << pDesc->server_level <<
			" opendays: " << pDesc->server_open_days);
	}
	return 0;
}

int ServerLevelDescEx::GetServerLevelByDays(int iDays) const
{
	int iIdx = 0;
	if(iDays <= 0)
	{
		iIdx = m_aiDaysIndex[1];
	}
	else if(iDays >= MAX_SERVER_LEVEL_DAYS_NUM)
	{
		iIdx = m_aiDaysIndex[MAX_SERVER_LEVEL_DAYS_NUM - 1];
	}
	else
	{
		iIdx = m_aiDaysIndex[iDays];
	}

	E_ServerLevel* pDesc = ServerLevelDesc::Instance()->GetDescByIndex(iIdx);
	CHECK_NULL(0, pDesc);
	return pDesc->server_level;
}
